package com.hzit.hzb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.hzit.common.result.R;
import com.hzit.hzb.base.util.JwtUtils;
import com.hzit.hzb.core.hfb.RequestHelper;
import com.hzit.hzb.core.service.UserAccountService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/userAccount")
@Slf4j
public class UserAccountController {

	@Autowired
	private UserAccountService userAccountService;

	// 这里进行异步回调
	@PostMapping("/auth/commitCharge/{chargeAmt}")
	public R commitCharge(@PathVariable("chargeAmt")BigDecimal chargeAmt, @RequestHeader("token") String token){
		// 1. 获取userId
		Long userId = JwtUtils.getUserId(token);
		// 2. 生成表单并提交
		String formStr = userAccountService.commitCharge(chargeAmt,userId);
		// 3. 返回表单串并在前端提交执行,跳转到汇付宝平台
		return R.ok().data("formStr",formStr);
	}

	// 汇付宝平台-->异步回调
	@ApiOperation(value = "用户充值异步回调")
	@PostMapping("/notify")
	public String notify(HttpServletRequest request){
		// 1. 处理请求参数
		Map<String, String[]> parameterMap = request.getParameterMap();
		Map<String, Object> paramMap = RequestHelper.switchMap(parameterMap);
		// 2. 进行验签处理
		if (RequestHelper.isSignEquals(paramMap)) {
			if (paramMap.get("resultCode").equals("0001")) {        // 证明返回的结果成功
				return userAccountService.notifyUserAccount(paramMap);
			}else{
				log.info("用户充值异步回调充值失败：" + JSON.toJSONString(paramMap));
				return "success";
			}
		}else{
			log.info("用户充值异步回调签名错误：" + JSON.toJSONString(paramMap));
			return "fail";
		}
	}

	//3. 获取账户余额
	@GetMapping("/getAccount")
	public R getAccount(@RequestHeader("token") String token){
		BigDecimal account = userAccountService.getAccount(JwtUtils.getUserId(token));
		return R.ok().data("account",account);
	}

	//4. 获取收益
	@GetMapping("/getInterestCount/{investAmount}/{lendYearRate}/{period}/{returnMethod}")
	public R getInterestCount(@PathVariable("investAmount") BigDecimal investAmount,
	                          @PathVariable("lendYearRate") BigDecimal lendYearRate,
	                          @PathVariable("period") Integer period,
	                          @PathVariable("returnMethod") Integer returnMethod
	                          ){
		// 获取收益
		BigDecimal interestCount = userAccountService.getInterestCount(investAmount,lendYearRate,period,returnMethod);
		// 返回
		return R.ok().data("interestCount",interestCount);
	}

	/**
	 * 用户提现
	 * @param fetchAmt
	 * @return
	 */
	@PostMapping("/auth/commitWithdraw/{fetchAmt}")
	public R commitWithdraw(@PathVariable("fetchAmt") BigDecimal fetchAmt,@RequestHeader("token") String token){
		// 1. 生成表单字符串
		String formStr = userAccountService.commitWithdraw(JwtUtils.getUserId(token),fetchAmt);
		// 2. 返回表单字符串并在前端执行，跳转到汇付宝平台
		return R.ok().data("formStr",formStr);
	}

	/**
	 * 提现异步回调地址
	 * @param request
	 * @return
	 */
	@PostMapping("/notifyWithdraw")
	public String notifyWithdraw(HttpServletRequest request){
		// 1. 处理获取的请求参数集合
		Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
		// 2. 进行验签
		if (RequestHelper.isSignEquals(paramMap)) {
			if("0001".equals(paramMap.get("resultCode").toString())){
				return userAccountService.notifyWithdraw(paramMap);
			}else {
				log.info("提现异步回调充值失败：" + JSON.toJSONString(paramMap));
				return "fail";
			}
		}else {
			log.info("提现异步回调签名错误：" + JSON.toJSONString(paramMap));
			return "fail";
		}
	}
}

